//
//  L0PlugSuitFlavor.m
//  PlugSuit
//
//  Created by ∞ on 10/3/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "L0PlugSuitFlavor.h"
#import <Carbon/Carbon.h>

int L0PlugSuitGetFlavor(pid_t pid) {
	// GetApplicationFlavor returns:
	//     -1 if the application flavor could not be identified
	//      0 if the application is a Mac OS Classic application
	//      2 if the application is a Carbon application
	//      3 if the application is a Cocoa application
	
	int flavor;
	OSStatus status;
	CFDictionaryRef processInfoDict = NULL;
	CFNumberRef processInfoFlavor = NULL;
	
	ProcessSerialNumber psn;
	status = GetProcessForPID(pid, &psn);
	require_noerr(status, GetCurrentProcess);
	
	processInfoDict = ProcessInformationCopyDictionary(&psn, kProcessDictionaryIncludeAllInformationMask);
	require(processInfoDict != NULL, ProcessInformationCopyDictionary);
	
	processInfoFlavor = CFDictionaryGetValue(processInfoDict, CFSTR("Flavor"));
	require(processInfoFlavor != NULL, CFDictionaryGetValue);
	
	CFNumberGetValue(processInfoFlavor, kCFNumberIntType, &flavor);
	
CFDictionaryGetValue:
ProcessInformationCopyDictionary:
GetCurrentProcess:
	
	if (processInfoFlavor != NULL)
		CFRelease(processInfoFlavor);
	if (processInfoDict != NULL)
		CFRelease(processInfoDict);
	
	return flavor;
}

BOOL L0PlugSuitIsCocoaApplication(pid_t pid) {
	return L0PlugSuitGetFlavor(pid) == L0PlugSuitCocoa;
}
